using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ChatboxSusieFountain : MonoBehaviour
{
	public TextMeshProUGUI TextUI;

	public AudioSource TextVoiceEmitter;

	private string CurrentText = "";

	public int CurrentTextIndex;

	public int CurrentAdditionalTextIndex;

	private int StoredAfterIndex;

	public int PauseCounter;

	public CHATBOXTEXT storedchatboxtext;

	public CHATBOXTEXT previouschatboxtext;

	public bool ChatIsCurrentlyRunning;

	private INT_Chat storedreciever;

	public TMP_FontAsset DefaultFont;

	public TMP_FontAsset DyslexicFont;

	public AudioClip DefaultSpeakSound;

	public Sprite DefaultIcon;

	private float CurrentTextSpeedMultiplier;

	private void Update()
	{
		ProcessInput();
		SetHeartCursorPOS();
	}

	private void ProcessInput()
	{
		if (CurrentText != "" || CurrentText != null)
		{
			PauseCounter = FormatCurrentText(CurrentText, IncludeBulletPoint: true).Length;
		}
		if (Input.GetKey((KeyCode)120) || Input.GetKey((KeyCode)303))
		{
			CurrentTextSpeedMultiplier = 0.33f;
		}
		else
		{
			CurrentTextSpeedMultiplier = 0.85f;
		}
	}

	private string FormatCurrentText(string TargetText, bool IncludeBulletPoint)
	{
		return TargetText.Replace("#", "").Replace("@", "").Replace(";", "\n")
			.Replace("~", "\n");
	}

	private void SetupChoices(CHATBOXTEXT chatbox, int index)
	{
	}

	private void CleanupOptions()
	{
	}

	private void SetHeartCursorPOS()
	{
	}

	public void RunText(CHATBOXTEXT Chatbox, int index, INT_Chat reciever, bool ResetCurrentTextIndex)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		if (ResetCurrentTextIndex)
		{
			CurrentTextIndex = 0;
		}
		((MonoBehaviour)this).StopCoroutine("PlayText");
		CleanupOptions();
		ChatIsCurrentlyRunning = true;
		CurrentAdditionalTextIndex = index;
		((Graphic)TextUI).color = Color.white;
		((Behaviour)TextUI).enabled = true;
		((TMP_Text)TextUI).text = "";
		StoredAfterIndex = index;
		storedchatboxtext = Chatbox;
		if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] != (Object)null && (Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFont != (Object)null)
		{
			((TMP_Text)TextUI).font = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFont;
		}
		else
		{
			((TMP_Text)TextUI).font = DefaultFont;
		}
		if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] != (Object)null && Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFontSize != 0f)
		{
			((TMP_Text)TextUI).fontSize = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFontSize;
		}
		else
		{
			((TMP_Text)TextUI).fontSize = 64f;
		}
		if (PlayerPrefs.GetInt("Setting_DyslexicText", 0) == 1)
		{
			((TMP_Text)TextUI).font = DyslexicFont;
		}
		((MonoBehaviour)this).StartCoroutine("PlayText");
	}

	public void EndText()
	{
		if ((Object)(object)storedreciever != (Object)null)
		{
			storedreciever.FinishedText = true;
		}
		((Behaviour)TextUI).enabled = false;
		StoredAfterIndex = 0;
		storedchatboxtext = null;
		CurrentText = "";
		PauseCounter = 0;
		SetHeartCursorPOS();
		CurrentTextIndex = 0;
		CurrentAdditionalTextIndex = 0;
		if ((Object)(object)storedreciever != (Object)null)
		{
			((MonoBehaviour)this).StartCoroutine(storedreciever.DebounceInteract());
		}
		storedreciever = null;
		((MonoBehaviour)this).StopCoroutine("PlayText");
		ChatIsCurrentlyRunning = false;
	}

	private IEnumerator PlayText()
	{
		string StoredText = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex];
		((TMP_Text)TextUI).text = StoredText;
		CurrentText = StoredText;
		((TMP_Text)TextUI).maxVisibleCharacters = 0;
		bool ForcedFinishText = false;
		int messageCharLength = StoredText.Length;
		char[] messageCharacters = StoredText.ToCharArray();
		if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action.Length != 0 && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex] != (Object)null && !storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex].RunActionOnChatEnd)
		{
			CHATBOXACTION cHATBOXACTION = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex];
			if (cHATBOXACTION.PlaySound && cHATBOXACTION.PossibleSounds.Length != 0)
			{
				AudioClip val = cHATBOXACTION.PossibleSounds[Random.Range(0, cHATBOXACTION.PossibleSounds.Length)];
				TextVoiceEmitter.PlayOneShot(val);
			}
			GameObject val2 = GameObject.Find(cHATBOXACTION.TargetComponentGameObjectName);
			if (cHATBOXACTION.RunComponentFunction && (Object)(object)val2 != (Object)null && cHATBOXACTION.FunctionName != null)
			{
				string targetComponentClassname = cHATBOXACTION.TargetComponentClassname;
				Component component = val2.GetComponent(targetComponentClassname);
				if ((Object)(object)component != (Object)null)
				{
					if (((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName) != null)
					{
						((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName).Invoke(component, null);
					}
					else
					{
						MonoBehaviour.print((object)"did you forget to make the method public?");
					}
				}
				else
				{
					MonoBehaviour.print((object)("Couldn't find Component named: " + targetComponentClassname));
				}
			}
		}
		while (((TMP_Text)TextUI).maxVisibleCharacters < messageCharLength)
		{
			if (messageCharacters[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == " ")
			{
				TextMeshProUGUI textUI = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI).maxVisibleCharacters;
				((TMP_Text)textUI).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.08f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (messageCharacters[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == "(")
			{
				TextMeshProUGUI textUI2 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI2).maxVisibleCharacters;
				((TMP_Text)textUI2).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.08f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (messageCharacters[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == ")")
			{
				TextMeshProUGUI textUI3 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI3).maxVisibleCharacters;
				((TMP_Text)textUI3).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.08f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == "@")
			{
				StoredText = RemoveSelectedCharacter(StoredText, ((TMP_Text)TextUI).maxVisibleCharacters);
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				yield return (object)new WaitForSeconds(0.125f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == "#")
			{
				StoredText = RemoveSelectedCharacter(StoredText, ((TMP_Text)TextUI).maxVisibleCharacters);
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				yield return (object)new WaitForSeconds(0.8f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == ";")
			{
				StoredText = RemoveSelectedCharacter(StoredText, ((TMP_Text)TextUI).maxVisibleCharacters);
				StoredText = AddSelectedCharacter(StoredText, ((TMP_Text)TextUI).maxVisibleCharacters, "\n");
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				TextMeshProUGUI textUI4 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI4).maxVisibleCharacters;
				((TMP_Text)textUI4).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.8f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == "~")
			{
				StoredText = RemoveSelectedCharacter(StoredText, ((TMP_Text)TextUI).maxVisibleCharacters);
				StoredText = AddSelectedCharacter(StoredText, ((TMP_Text)TextUI).maxVisibleCharacters, "\n");
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				TextMeshProUGUI textUI5 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI5).maxVisibleCharacters;
				((TMP_Text)textUI5).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.8f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == ",")
			{
				TextMeshProUGUI textUI6 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI6).maxVisibleCharacters;
				((TMP_Text)textUI6).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.2f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == ".")
			{
				TextMeshProUGUI textUI7 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI7).maxVisibleCharacters;
				((TMP_Text)textUI7).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.08f * CurrentTextSpeedMultiplier);
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == "<")
			{
				TextMeshProUGUI textUI8 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI8).maxVisibleCharacters;
				((TMP_Text)textUI8).maxVisibleCharacters = maxVisibleCharacters + 1;
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == ">")
			{
				TextMeshProUGUI textUI9 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI9).maxVisibleCharacters;
				((TMP_Text)textUI9).maxVisibleCharacters = maxVisibleCharacters + 1;
				continue;
			}
			if (StoredText[((TMP_Text)TextUI).maxVisibleCharacters].ToString() == "£")
			{
				StoredText = RemoveSelectedCharacter(StoredText, ((TMP_Text)TextUI).maxVisibleCharacters);
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				ForcedFinishText = true;
				continue;
			}
			if (Object.op_Implicit((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterSound))
			{
				TextVoiceEmitter.PlayOneShot(storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterSound);
			}
			if (!ForcedFinishText)
			{
				TextMeshProUGUI textUI10 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI10).maxVisibleCharacters;
				((TMP_Text)textUI10).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.08f * CurrentTextSpeedMultiplier);
			}
			else
			{
				TextMeshProUGUI textUI11 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI11).maxVisibleCharacters;
				((TMP_Text)textUI11).maxVisibleCharacters = maxVisibleCharacters + 1;
			}
		}
		if (CurrentTextSpeedMultiplier == 0.85f)
		{
			yield return (object)new WaitForSeconds(2f);
		}
		else
		{
			yield return (object)new WaitForSeconds(0.5f);
		}
		if (((TMP_Text)TextUI).maxVisibleCharacters == PauseCounter && (Object)(object)storedchatboxtext != (Object)null)
		{
			if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action.Length != 0 && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex] != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex].RunActionOnChatEnd)
			{
				CHATBOXACTION cHATBOXACTION2 = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex];
				if (cHATBOXACTION2.PlaySound && cHATBOXACTION2.PossibleSounds.Length != 0)
				{
					AudioClip val3 = cHATBOXACTION2.PossibleSounds[Random.Range(0, cHATBOXACTION2.PossibleSounds.Length)];
					TextVoiceEmitter.PlayOneShot(val3);
				}
				GameObject val4 = GameObject.Find(cHATBOXACTION2.TargetComponentGameObjectName);
				if (cHATBOXACTION2.RunComponentFunction && (Object)(object)val4 != (Object)null && cHATBOXACTION2.FunctionName != null)
				{
					string targetComponentClassname2 = cHATBOXACTION2.TargetComponentClassname;
					Component component2 = val4.GetComponent(targetComponentClassname2);
					if ((Object)(object)component2 != (Object)null)
					{
						if (((object)component2).GetType().GetMethod(cHATBOXACTION2.FunctionName) != null)
						{
							((object)component2).GetType().GetMethod(cHATBOXACTION2.FunctionName).Invoke(component2, null);
						}
						else
						{
							MonoBehaviour.print((object)"did you forget to make the method public?");
						}
					}
					else
					{
						MonoBehaviour.print((object)("Couldn't find Component named: " + targetComponentClassname2));
					}
				}
			}
			if (CurrentTextIndex < storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text.Length - 1)
			{
				CurrentTextIndex++;
				RunText(storedchatboxtext, StoredAfterIndex, storedreciever, ResetCurrentTextIndex: false);
			}
			else
			{
				EndText();
			}
		}
		else
		{
			if (((TMP_Text)TextUI).maxVisibleCharacters != PauseCounter)
			{
				yield break;
			}
			if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action.Length != 0 && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex] != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex].RunActionOnChatEnd)
			{
				CHATBOXACTION cHATBOXACTION3 = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex];
				if (cHATBOXACTION3.PlaySound && cHATBOXACTION3.PossibleSounds.Length != 0)
				{
					AudioClip val5 = cHATBOXACTION3.PossibleSounds[Random.Range(0, cHATBOXACTION3.PossibleSounds.Length)];
					TextVoiceEmitter.PlayOneShot(val5);
				}
				GameObject val6 = GameObject.Find(cHATBOXACTION3.TargetComponentGameObjectName);
				if (cHATBOXACTION3.RunComponentFunction && (Object)(object)val6 != (Object)null && cHATBOXACTION3.FunctionName != null)
				{
					string targetComponentClassname3 = cHATBOXACTION3.TargetComponentClassname;
					Component component3 = val6.GetComponent(targetComponentClassname3);
					if ((Object)(object)component3 != (Object)null)
					{
						if (((object)component3).GetType().GetMethod(cHATBOXACTION3.FunctionName) != null)
						{
							((object)component3).GetType().GetMethod(cHATBOXACTION3.FunctionName).Invoke(component3, null);
						}
						else
						{
							MonoBehaviour.print((object)"did you forget to make the method public?");
						}
					}
					else
					{
						MonoBehaviour.print((object)("Couldn't find Component named: " + targetComponentClassname3));
					}
				}
			}
			EndText();
		}
	}

	private string RemoveSelectedCharacter(string text, int Index)
	{
		return text.Remove(Index, 1);
	}

	private string AddSelectedCharacter(string text, int Index, string Insert)
	{
		return text.Insert(Index, Insert);
	}

	public int CountOfStringInText(string TargetText, char Counter)
	{
		int num = 0;
		bool flag = false;
		char[] array = TargetText.ToCharArray();
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i] == Counter)
			{
				flag = true;
			}
			else if (false)
			{
				num++;
			}
		}
		return num;
	}

	private IEnumerator PlayTextBackup()
	{
		string StoredText = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex];
		((TMP_Text)TextUI).text = "* ";
		string currentText = CurrentText;
		for (int i = 0; i < currentText.Length; i++)
		{
			char c = currentText[i];
			if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex] == StoredText)
			{
				if (c.ToString() == "#")
				{
					yield return (object)new WaitForSeconds(0.5f);
				}
				else if (c.ToString() == "@")
				{
					yield return (object)new WaitForSeconds(0.125f);
				}
				else if (c.ToString() == ",")
				{
					TextMeshProUGUI textUI = TextUI;
					((TMP_Text)textUI).text = ((TMP_Text)textUI).text + c;
					yield return (object)new WaitForSeconds(0.2f);
				}
				else if (c.ToString() == ".")
				{
					TextMeshProUGUI textUI2 = TextUI;
					((TMP_Text)textUI2).text = ((TMP_Text)textUI2).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else if (c.ToString() == "?")
				{
					TextMeshProUGUI textUI3 = TextUI;
					((TMP_Text)textUI3).text = ((TMP_Text)textUI3).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else if (c.ToString() == "!")
				{
					TextMeshProUGUI textUI4 = TextUI;
					((TMP_Text)textUI4).text = ((TMP_Text)textUI4).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else if (c.ToString() == " ")
				{
					TextMeshProUGUI textUI5 = TextUI;
					((TMP_Text)textUI5).text = ((TMP_Text)textUI5).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else
				{
					TextMeshProUGUI textUI6 = TextUI;
					((TMP_Text)textUI6).text = ((TMP_Text)textUI6).text + c;
					TextVoiceEmitter.PlayOneShot(TextVoiceEmitter.clip);
					yield return (object)new WaitForSeconds(0.02f);
				}
			}
		}
	}
}
